package com.xn.app.muying.adpter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 文章适配器
 * Created by gaoge on 2017/8/13.
 */

public class ArticleAdapter extends BaseAdapter {
    LayoutInflater lf = null;
    ArrayList<HashMap<String, String>> getData = new ArrayList<HashMap<String, String>>();

    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public ArticleAdapter(Context context,
                          ArrayList<HashMap<String, String>> getData, ImageLoader loader,
                          DisplayImageOptions options) {
        lf = LayoutInflater.from(context);
        this.imageLoader = loader;
        this.options = options;
        this.getData = getData;
    }

    @Override
    public int getCount() {
        return getData.size();
    }

    @Override
    public Object getItem(int position) {
        return getData.get(position);
    }

    public void getData(ArrayList<HashMap<String, String>> data) {
        this.getData = data;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = lf.inflate(R.layout.education_list_item, null);
            viewHolder.imageview = (ImageView) convertView.findViewById(R.id.imageview);
            viewHolder.textview_title = (TextView) convertView.findViewById(R.id.textview_title);
            viewHolder.textview_content = (TextView) convertView.findViewById(R.id.textview_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textview_title.setText(getData.get(position).get("title"));
        viewHolder.textview_content.setText(getData.get(position).get("shortDescription"));
        //设置图片，如果内存中有，优先从内存中读取
        viewHolder.imageview.setImageResource(0);
        if (getData.get(position).get("pictureUrl") != null) {
            imageLoader.displayImage(getData.get(position).get("pictureUrl"), viewHolder.imageview, options);
        }
        return convertView;
    }

    /**
     * 包装类
     */
    class ViewHolder {
        private ImageView imageview = null;
        private TextView textview_title = null;
        private TextView textview_content = null;
    }
}


